<template>
	<div class="home-content">
		<uv-input placeholder="请扫描或输入券码" type="number" :customStyle="customInputStyle">
			<template v-slot:prefix>
				<uv-text text="券码:" :bold="true" color="#000000" margin="0 3px 0 0"></uv-text>
			</template>
			<template v-slot:suffix>
				<uv-button shape="circle" @click="getCode" text="确定" size="mini"></uv-button>
			</template>
		</uv-input>

		<div class="btn-center" @click="scanCode">
			<i class="iconfont icon-icon2"></i>
			<div class="text">扫码核销</div>
		</div>
	</div>
	<!-- <uv-tabbar :value="value" :fixed="true" @change="(index) => (value = index)" :customStyle="customStyle">
		<uv-tabbar-item text="核销" icon="scan" style="flex: 1;"></uv-tabbar-item>
		<uv-tabbar-item text="统计" icon="empty-news" style="flex: 1;"></uv-tabbar-item>
	</uv-tabbar> -->
</template>
<script>
import lottie from "lottie-miniprogram";
import success from "./success.json";
export default {
	data() {
		return {
			value: 0,
			customInputStyle: {
				height: "80rpx",
				marginTop: "30rpx",
			},
			customStyle: {
				paddingTop: "20rpx",
			},
		};
	},
	methods: {
		getUrlParams(urlStr, name) {
			let url = urlStr.substring(urlStr.indexOf("?"));
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = url.substr(1).match(reg);
			// var r = window.location.search.substr(1).match(reg);
			if (r != null) return unescape(r[2]);
			return null;
		},
        getCode(){
            uni.reLaunch({
				url: "/pages/login",
			});
        },
		scanCode() {
			uni.scanCode({
				success: (res) => {
                    console.log(res);
					if ((res && res.result.includes("http")) || res.result.includes("https")) {
						const orgId = this.getUrlParams(res.result, "orgId");
                        console.log(orgId);
						if (orgId && orgId !== "undefined") {
							if (res.result.indexOf("/book/book-info") !== -1) {
								let bookId = this.getUrlParams(res.result, "bookId");
								let bookCatId = this.getUrlParams(res.result, "bookCatId");
								this.switchOrgWithF(orgId, function() {
									uni.navigateTo({
										url: `/pageSubcontract/book/book-info?bookId=${bookId}&&bookCatId=${bookCatId}`,
									});
								});
							} else if (res.result.indexOf("/video/video-info") !== -1) {
								let id = this.getUrlParams(res.result, "id");
								this.switchOrgWithF(orgId, function() {
									uni.navigateTo({
										url: `/pageSubcontract/video/video-info?id=${id}`,
									});
								});
							} else if (res.result.indexOf("/audio/audio-info") !== -1) {
								let id = this.getUrlParams(res.result, "id");
								this.switchOrgWithF(orgId, function() {
									uni.navigateTo({
										url: `/pageSubcontract/audio/audio-info?id=${id}`,
									});
								});
							} else if (res.result.indexOf("/information/information-detail") !== -1) {
								let articleId = this.getUrlParams(res.result, "articleId");
								this.switchOrgWithF(orgId, function() {
									uni.navigateTo({
										url: `/pageSubcontract/information/information-detail?articleId=${articleId}`,
									});
								});
							} else if (res.result.indexOf("/activity/activity-detail") !== -1) {
								let activityId = this.getUrlParams(res.result, "activityId");
								this.switchOrgWithF(orgId, function() {
									uni.navigateTo({
										url: `/pageSubcontract/activity/activity-detail?activityId=${activityId}`,
									});
								});
							} else if (res.result.indexOf("/journal/journal-info") !== -1) {
								uni.showToast({
									title: "小程序暂不支持期刊阅读",
									icon: "none",
								});
							} else {
								uni.showToast({
									title: "未识别到本平台资源信息",
									icon: "none",
								});
							}
						} else {
							uni.showToast({
								title: "未识别到机构信息",
								icon: "none",
							});
						}
					}
				},
			});
		},
	},
};
</script>
<style lang="scss" scoped>
.home-content {
	padding: 30rpx;
	.btn-center {
		color: white;
		background-image: linear-gradient(-60deg, #ff5858 0%, #f09819 100%);
		width: 150rpx;
		margin: 120rpx auto;
		height: 150rpx;
		padding: 50rpx;
		border-radius: 150rpx;
		text-align: center;
		display: flex;
		align-items: center;
		flex-direction: column;
		.iconfont {
			font-size: 60rpx;
		}
		.text {
			margin-top: 20rpx;
		}
	}
}
</style>
